﻿using System.Diagnostics;
using System.Windows;
using HslCommunication.ModBus;

namespace ModbusTcpTest;

/// <summary>
///     Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    // private ModbusTcpNet busTcpClient = new ModbusTcpNet("192.168.1.222", 502, 0x01);   // 站号1
    private readonly ModbusTcpNet busTcpClient = new("192.168.1.222"); // 端口号502，站号1

    public MainWindow()
    {
        InitializeComponent();
        busTcpClient.AddressStartWithZero = false;
    }

    private void BtnConnect_OnClick(object sender, RoutedEventArgs e)
    {
        var r = busTcpClient.ConnectServer();
        MessageBox.Show(r.IsSuccess ? "成功连接" : "连接失败");
    }

    private void BtnDisconnect_OnClick(object sender, RoutedEventArgs e)
    {
        busTcpClient.ConnectClose();
    }

    private void BtnWrite_OnClick(object sender, RoutedEventArgs e)
    {
        for (var i = 0; i < 10000; i++)
        {
            // 写入操作
            // busTcpClient.WriteCoil("100", true); // 写入线圈100为通
            // busTcpClient.Write("100", (short)12345); // 写入寄存器100为12345
            // busTcpClient.Write("100", (ushort)45678); // 写入寄存器100为45678
            var r=busTcpClient.Write("10",i); // 写入寄存器100-101为123456789
            
            Debug.WriteLine(r.IsSuccess?$@"写入 {i} 次":$@"写入 {i} 失败");
            // busTcpClient.Write("100", (uint)123456778); // 写入寄存器100-101为123456778
            // busTcpClient.Write("100", 123.456); // 写入寄存器100-101为123.456
            // busTcpClient.Write("100", 12312312312414L); //写入寄存器100-103为一个大数据
            // busTcpClient.Write("100", 12634534534543656UL); // 写入寄存器100-103为一个大数据
            // busTcpClient.Write("100", 123.456d); // 写入寄存器100-103为一个双精度的数据
            // busTcpClient.Write("100", "K123456789");

            var int100 = busTcpClient.ReadInt32("10").Content; // 读取寄存器100-101的int值
            Debug.WriteLine($"读取 {i}: {int100}");
        }
    }

    private void BtnRead_OnClick(object sender, RoutedEventArgs e)
    {
        for (var i = 0; i < 1; i++)
        {
            // 读取操作
            // var read100_1 = busTcpClient.ReadBool("100.1").Content; // 读取寄存器100的第0位通断　　
            // var coil100 = busTcpClient.ReadCoil("100").Content; // 读取线圈100的通断
            // var short100 = busTcpClient.ReadInt16("100").Content; // 读取寄存器100的short值
            // var ushort100 = busTcpClient.ReadUInt16("100").Content; // 读取寄存器100的ushort值
            var int100 = busTcpClient.ReadInt32("1").Content; // 读取寄存器100-101的int值
            Debug.WriteLine($"读取 {i}: {int100}");
            // var uint100 = busTcpClient.ReadUInt32("100").Content; // 读取寄存器100-101的uint值
            // var float100 = busTcpClient.ReadFloat("100").Content; // 读取寄存器100-101的float值
            // var long100 = busTcpClient.ReadInt64("100").Content; // 读取寄存器100-103的long值
            // var ulong100 = busTcpClient.ReadUInt64("100").Content; // 读取寄存器100-103的ulong值
            // var double100 = busTcpClient.ReadDouble("100").Content; // 读取寄存器100-103的double值
            // var str100 = busTcpClient.ReadString("100", 5).Content; // 读取100到104共10个字符的字符串
        }
    }
}